package com.dycong.common.leetcode.sort;

import java.util.Arrays;

public class ChoiceSort {

    public static void main(String[] args) {
        ChoiceSort bubbleSort = new ChoiceSort();
        int[] ints = bubbleSort.sort(new int[]{1, 2, 5, 8, 6, 5, 9, 5, 55, 5});
        System.out.println(Arrays.toString(ints));
    }


    public int[] sort(int[] nums) {
        if (nums.length == 0) {
            return nums;
        }

        for (int i = 0; i < nums.length; i++) {
            int min = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[min] > nums[j]) {
                    min = j;
                }
            }
            if (i != min) {
                swap(nums, i, min);
            }
        }
        return nums;
    }

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
